using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TMCompositeTitle : MonoBehaviour
{
    GameObject m_RowRaw;

    List<TMCompositeText> m_Texts = new List<TMCompositeText>();
    int m_CurShowRow = 0;

    public string TermKey;
    void Awake()
    {
        m_RowRaw = transform.Find("Row").gameObject;
        m_RowRaw.SetActive(false);
    }

    void Start()
    {
        if (!string.IsNullOrEmpty(TermKey))
        {
            SetText(TermKey);
        }
    }

    public void SetText(string text)
    {
        string[] ths = text.Split('\n');
        if (ths.Length == 1)
        {
            m_RowRaw.SetActive(true);
            m_RowRaw.GetComponent<TMCompositeText>().SetText(ths[0]);
            m_RowRaw.GetComponent<TMCompositeText>().Show(null);
        }
        else
        {
            float row_interval = 0;
            for (int i = 0; i < ths.Length; i++)
            {
                GameObject go = GameObject.Instantiate(m_RowRaw, transform);
                go.SetActive(true);
                var tt = go.GetComponent<TMCompositeText>();
                tt.SetText(ths[i]);
                m_Texts.Add(tt);

                if (i == 0)
                {
                    row_interval = tt.RowHeight;
                }
                else
                {
                    tt.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -i * row_interval);
                }
            }

            m_Texts[m_CurShowRow].Show(OnRowShowEnd);
        }
    }

    void OnRowShowEnd()
    {
        m_CurShowRow++;
        if (m_CurShowRow < m_Texts.Count)
        {
            m_Texts[m_CurShowRow].Show(OnRowShowEnd);
        }
    }
}
